package ebcs.scan.service.wechat;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;

import ebcs.scan.common.utils.HttpClientUtil;
import ebcs.scan.common.utils.Properties;
import  ebcs.scan.common.utils.StringUtil;
import jplat.core.trans.JAppBaseService;
import jplat.tools.config.JBusConfig;

/***
 * 微信服务
 * @author chenzhuo
 * @comment
 */
@Service
public class WechatCtrlService extends JAppBaseService
{
	private Logger logger = Logger.getLogger(WechatCtrlService.class);
   
	 
	/**
	 * 
	 * @param redirect_uri
	 * @param state
	 * @return
	 * @throws IOException
	 */
	public String buildAuthorizeUrl(String redirect_uri, String state) throws IOException {
		StringBuilder sb = new StringBuilder();
 		sb.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
 		String wechat_appId = JBusConfig.WECHAT_APPID;
  		sb.append("appid=").append(wechat_appId);
 		sb.append("&redirect_uri=").append(URLEncoder.encode(redirect_uri, "utf-8"));
 		sb.append("&response_type=code&scope=snsapi_base&state=").append(URLEncoder.encode(state, "utf-8"));
		sb.append("#wechat_redirect");
		logger.info("1.wechat buildAuthorizeUrl:"+sb.toString());
		return sb.toString();
	}
	//获取openid
	public String getUserInfoByCode(String code){
		StringBuilder sb = new StringBuilder();
		sb.append("https://api.weixin.qq.com/sns/oauth2/access_token?grant_type=authorization_code");
		String wechat_appId = JBusConfig.WECHAT_APPID;
  		sb.append("&appid=").append(JBusConfig.WECHAT_APPID);
		sb.append("&secret=").append(JBusConfig.WECHAT_APPSECRET);
 		sb.append("&code=").append(code);
		logger.info("2.wechat getUserInfoByCode:"+sb.toString());
		return sb.toString();
	}
	   
}
